// do /Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Code_JPEmicro/Extract_Fox_theta_4q_avg_bs.do
//Options: discrete 44000 16 1

// Dependency: /Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Code_JPEmicro/plotting/plot_Fox_contour_3dim_bs.do 

pause on


global  pathresults = "/Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Results_estimation"
global  Est_pathresults = "/Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Data/Matlab_estimation"

local FOX_model = `"`1'"'   // discrete, 
local subsample = `"`2'"'   // Use 44000 
local inc       = `"`3'"'   // 16 1  2 3 4 5 6
local set_seed  = `"`4'"'   // 1  10 3 6 4 3 7 

insheet using $Est_pathresults/Fox_`FOX_model'_mixedFEdemoshrt_WTP_v`subsample'_inc`inc'_sd`set_seed'_cty_tau1_grid_v3_all_bs.csv, clear


 forvalues i=1(1)16 { 
	capture destring weight`i', replace force 
 }
 
 collapse(sum) weight*, by(theta eta)
 
 gen theta_4q =. 
 replace theta_4q = 1 if theta < 0
 replace theta_4q = 2 if theta >= 0 & theta<0.75
 replace theta_4q = 3 if theta >= 0.75 & theta < 1.25
 replace theta_4q = 4 if theta >= 1.25
 
//forvalues i=1(1)10 {
//forvalues i=1(1)7 { 
forvalues i=1(1)16 { 
 capture gen eta_avg_tmp`i' = eta*weight`i' 
 capture gen theta_avg_tmp`i' = theta*weight`i' 
 //capture gen weight_100`i' = round(100*weight`i')
 capture egen eta_avg`i' = sum(eta_avg_tmp`i') 
 capture egen theta_avg`i' = sum(theta_avg_tmp`i')
 
}  

collapse(sum) weight*, by(theta_4q)

egen weight_avg=rowmean(weight*)
 
keep theta_4q weight_avg
ren weight_avg mean_weight_`inc'
sort theta_4q

save $pathresults/Theta_pdf_4q_Fox_`FOX_model'_v`subsample'_inc`inc'_sd`set_seed', replace 


